package com.one.efaimaly.order.ui;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.imagchoose.ChooseManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.photolayout.SortableNinePhotoLayout;
import com.one.common.view.preview.PreviewPicActivity;
import com.one.common.view.widget.MyTitleBar;
import com.one.efaimaly.R;
import com.one.efaimaly.order.presenter.UploadImgPresenter;
import com.one.efaimaly.order.view.UploadImgView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: zhulunjun
 * Time  : 2018/10/19 12:01
 * email : zhulunjun@qq.com
 * Describe : 上传维修图片
 **/
@Route(path = RouterPath.UPLOAD_IMG)
public class UploadImageActivity extends BaseActivity<UploadImgPresenter> implements UploadImgView, SortableNinePhotoLayout.Delegate {
    @BindView(R.id.snp_photo_1)
    SortableNinePhotoLayout snpPhoto1;
    @BindView(R.id.snp_photo_2)
    SortableNinePhotoLayout snpPhoto2;

    /**
     * 已维修
     */
    private static final int TYPE_ALREADY = 1;
    /**
     * 未维修
     */
    private static final int TYPE_NOT = -1;
    @BindView(R.id.tv_btn_confirm)
    TextView tvBtnConfirm;
    private int type;

    @Override
    protected void initPresenter() {
        mPresenter = new UploadImgPresenter(this,this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_upload_img;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("上传图片");
    }

    @Override
    public void initView() {
        super.initView();
        snpPhoto1.setDelegate(this);
        snpPhoto2.setDelegate(this);
    }

    @Override
    public void onClickAddNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
        if (sortableNinePhotoLayout.getId() == R.id.snp_photo_1) {
            type = TYPE_NOT;
        } else {
            type = TYPE_ALREADY;
        }
        ChooseManager.getChooser().chooseSingle(this);
    }


    @Override
    public void onClickDeleteNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        if (sortableNinePhotoLayout.getId() == R.id.snp_photo_1) {
            snpPhoto1.removeItem(position);
        } else {
            snpPhoto2.removeItem(position);
        }
    }

    @Override
    public void onClickNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        PreviewPicActivity.starPreview(this,sortableNinePhotoLayout.getData(), position);
    }

    @Override
    public void onNinePhotoItemExchanged(SortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                ArrayList<String> pathList =
                        data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
                if (pathList != null && pathList.size() > 0) {
                    if (type == TYPE_NOT) {
                        snpPhoto1.addLastItem(pathList.get(0));
                    } else {
                        snpPhoto2.addLastItem(pathList.get(0));
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 显示维修图片，已经上传过图片了
     */
    @Override
    public void showImg(ArrayList<String> img1, ArrayList<String> img2) {
        tvBtnConfirm.setVisibility(View.GONE);
        snpPhoto1.setEditable(false);
        snpPhoto2.setEditable(false);
        snpPhoto1.setData(img1);
        snpPhoto2.setData(img2);

    }

    @OnClick(R.id.tv_btn_confirm)
    public void submit(){
        mPresenter.uploadImg(snpPhoto1.getData(), snpPhoto2.getData());
    }

}
